package com.chrm.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 *
 *  @Author: Darren 周金涛
 *
 *  @Description: 整合 Swagger
 *
 *  @Date: 2019/09/07 1:52
 *
 */
public class SwaggerPlugin extends PluginAdapter {

    private FullyQualifiedJavaType apiModelAnnotation;
    private FullyQualifiedJavaType apiModelPropertyAnnotation;

    public SwaggerPlugin() {
        apiModelAnnotation = new FullyQualifiedJavaType("io.swagger.annotations.ApiModel");
        apiModelPropertyAnnotation = new FullyQualifiedJavaType("io.swagger.annotations.ApiModelProperty");
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    /**
     * 拦截 普通字段
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        addApiModelAnnotationAnnotation(topLevelClass,introspectedTable);
        return true;
    }

    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass,
                                       IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable,
                                       ModelClassType modelClassType) {

        if(introspectedColumn != null && introspectedColumn.getRemarks() != null){
            field.addAnnotation(String.format("@ApiModelProperty(name = \"%s\")",introspectedColumn.getRemarks().toString()));
        }else{
            field.addAnnotation(String.format("@ApiModelProperty"));
        }
        return true;
    }

    /**
     * 拦截 主键
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        addApiModelAnnotationAnnotation(topLevelClass,introspectedTable);
        return true;
    }

    /**
     * 拦截 blob 类型字段
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelRecordWithBLOBsClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        addApiModelAnnotationAnnotation(topLevelClass,introspectedTable);
        return true;
    }

    /**
     * Prevents all getters from being generated.
     * See SimpleModelGenerator
     *
     * @param method
     * @param topLevelClass
     * @param introspectedColumn
     * @param introspectedTable
     * @param modelClassType
     * @return
     */
    @Override
    public boolean modelGetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable,
                                              ModelClassType modelClassType) {
        return false;
    }

    /**
     * Prevents all setters from being generated
     * See SimpleModelGenerator
     *
     * @param method
     * @param topLevelClass
     * @param introspectedColumn
     * @param introspectedTable
     * @param modelClassType
     * @return
     */
    @Override
    public boolean modelSetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable,
                                              ModelClassType modelClassType) {
        return false;
    }


    /**
     *  Adds @ApiModel import and annotation to the class
     * @param topLevelClass
     * @param introspectedTable
     */
    protected void addApiModelAnnotationAnnotation(TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {
        System.out.println(introspectedTable.toString());
        topLevelClass.addImportedType(apiModelAnnotation);
        topLevelClass.addImportedType(apiModelPropertyAnnotation);
        if(introspectedTable != null && introspectedTable.getRemarks() != null && introspectedTable.getRemarks() != ""){
            System.out.println(introspectedTable.getRemarks());
            topLevelClass.addAnnotation(String.format("@ApiModel(value = \""+ introspectedTable.getRemarks()+"\")"));
        }else{
            topLevelClass.addAnnotation(String.format("@ApiModel"));
        }

    }

}
